﻿namespace Flash.Web
{
    using CachePage;
    using Flash.Web.UI;
    using FluorineFx;
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Caching;
    using System.Web.UI;

    [RemotingService]
    public class FlashRemoting
    {
        private string _remoteId;
        public const string Create = "$Create";

        public Dictionary<string, object> CreateControl(string remoteId, string controlPath)
        {
            this._remoteId = remoteId;
            this.ControlPath = controlPath;
            return this.Remote.ListenEvent("$Create", new Dictionary<string, object>());
        }

        public string NewRemoteID()
        {
            return Guid.NewGuid().ToString();
        }

        public Dictionary<string, object> SendEvent(string remoteId, string name, Dictionary<string, object> value)
        {
            this._remoteId = remoteId;
            return this.Remote.ListenEvent(name, value);
        }

        private System.Web.Caching.Cache Cache
        {
            get
            {
                return HttpContext.Current.Cache;
            }
        }

        private string ControlPath
        {
            get
            {
                return (string) this.Cache["controlPath_" + this._remoteId];
            }
            set
            {
                TimeSpan cacheTimeOut = ExtensionsConfigSection.GetCacheTimeOut();
                if (cacheTimeOut != TimeSpan.Zero)
                {
                    this.Cache.Insert("controlPath_" + this._remoteId, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, cacheTimeOut);
                }
                else
                {
                    this.Cache["controlPath_" + this._remoteId] = value;
                }
            }
        }

        private Consignee Remote
        {
            get
            {
                return new Consignee(this.ControlPath, this._remoteId);
            }
        }

        private class Consignee : TemplateControl
        {
            private string _controlPath;
            private string _remoteId;
            private TimeSpan controlLimit;

            public Consignee(string controlPath, string remoteId)
            {
                this._controlPath = controlPath;
                this._remoteId = remoteId;
                TimeSpan cacheTimeOut = ExtensionsConfigSection.GetCacheTimeOut();
                if (cacheTimeOut != TimeSpan.Zero)
                {
                    this.controlLimit = cacheTimeOut;
                }
                else
                {
                    this.controlLimit = new TimeSpan(0, 40, 0);
                }
            }

            public Dictionary<string, object> ListenEvent(string name, Dictionary<string, object> value)
            {
                UserControl control = (UserControl) base.LoadControl(this._controlPath);
                control.ID = "Flash_Root";
                control.SetRemoteID(this._remoteId);
                if (name != "$Create")
                {
                    UserControl.LoadFlashViewState(control, (Pair) this.Cache["FlashState_" + this._remoteId], name, value);
                }
                Dictionary<string, object> dictionary = control.ListenEvent(name, value);
                this.Cache.Insert("FlashState_" + this._remoteId, control.FlashState, null, System.Web.Caching.Cache.NoAbsoluteExpiration, this.controlLimit);
                return dictionary;
            }

            private System.Web.Caching.Cache Cache
            {
                get
                {
                    return HttpContext.Current.Cache;
                }
            }
        }
    }
}

